home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d18 / turbotut.arc / MANUAL.EXE / lha / CHAP2.TXT < prev    next >
Text File  |  1988-11-27  |  14KB  |  322 lines

  1.                    CHAPTER 2 - Getting started in Pascal
  2.  
  3.  
  4.                          YOUR FIRST PASCAL PROGRAM
  5.  
  6.             Lets  get right into a program that really does  nothing
  7.         but  is an example of the most trivial Pascal program.  Load
  8.         Turbo  Pascal,  select TRIVIAL as a Work  file,  and  select
  9.         Edit. This assumes that you have been successful in learning
  10.         how to use the TURBO Pascal system.
  11.  
  12.             You  should  now  have the most trivial  Pascal  program
  13.         possible  on your display,  and we can take a look  at  each
  14.         part to define what it does.
  15.  
  16.             The  first  line  is  required in  the  standard  Pascal
  17.         definition and is the program name which can be any name you
  18.         like,  as  long  as it follows the rules for  an  identifier
  19.         given  in  the  next  paragraph.  It  can  have  no  blanks,
  20.         otherwise  it would be considered as two words and it  would
  21.         confuse the compiler. The first word PROGRAM is the first of
  22.         the reserved words mentioned earlier and it is the indicator
  23.         to the Pascal compiler that this is the name of the program.
  24.         Notice that the line ends with a semicolon.  Pascal uses the
  25.         semicolon  as  the  statement  separator  and  although  all
  26.         statements do not actually end in a semicolon,  most do, and
  27.         use of the semicolon will clear up later in your mind. TURBO
  28.         Pascal does not require the PROGRAM statement, but to remain
  29.         compatible with standard Pascal,  it will simply ignore  the
  30.         entire  statement.  I like to include a program name both to
  31.         keep  me thinking in standard Pascal,  and to add  a  little
  32.         more indication of the purpose of each program.
  33.  
  34.                           WHAT IS AN IDENTIFIER?
  35.  
  36.             All identifiers,  including program name,  procedure and
  37.         function names,  type definitions, and constant and variable
  38.         names,  will  start  with an alphabetical character  and  be
  39.         composed  of  any  combination  of  alphabetic  and  numeric
  40.         characters  with  no embedded blanks.  Upper or  lower  case
  41.         alphabetic  characters are not significant and may be  mixed
  42.         at  will.  (If  you find this definition confusing  at  this
  43.         point,  don't worry about it,  it will be clear later but it
  44.         must  be defined early).  The standard definition of  Pascal
  45.         requires that any implementation (i.e.  any compiler written
  46.         by  some  company)  must use at least 8  characters  of  the
  47.         identifier  as  significant  and may  ignore  the  remaining
  48.         characters  if more are used.  Most implementations use  far
  49.         more  than 8.  TURBO Pascal uses up to 127 characters in  an
  50.         identifier  as  being significant.  Since nearly all  Pascal
  51.         compilers use the underline as an allowable character in  an
  52.         identifier, it will be freely used throughout this tutorial.
  53.         The  underline is used in the program name "puppy_dog" which
  54.         should be on your display at this time.
  55.  
  56.  
  57.                                  Page 6
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                    CHAPTER 2 - Getting started in Pascal
  68.  
  69.  
  70.  
  71.             Returning  to the example program,  the next line  is  a
  72.         blank  line which is ignored by all Pascal  compilers.  More
  73.         will be said about that at the end of this chapter.
  74.  
  75.                             NOW FOR THE PROGRAM
  76.  
  77.             The  next two lines comprise the actual Pascal  program,
  78.         which  in  this  case  does absolutely  nothing.  It  is  an
  79.         illustration  of the minimum Pascal program.  The two  words
  80.         BEGIN  and  END  are the next two  reserved  words  we  will
  81.         consider.  Any  logical  grouping  of  Pascal  code  can  be
  82.         isolated  by bracketing it with the two reserved words BEGIN
  83.         and END. You will use this construct repeatedly as you write
  84.         Pascal code so it is well to learn it thoroughly. Code to be
  85.         executed by conditional jumps will be bracketed by BEGIN and
  86.         END, as will code within a loop, and code contained within a
  87.         subroutine (although they are called PROCEDURES in  Pascal),
  88.         and  in many other ways.  In the present program,  the BEGIN
  89.         and  END  are  used to bracket the main  program  and  every
  90.         Pascal  program will have the main program bracketed by  the
  91.         BEGIN  and END statement.  Since there is nothing to  do  in
  92.         this program, there are no statements.
  93.  
  94.             Finally,  although  it could be very easily  overlooked,
  95.         there  is one more very important part of the  program,  the
  96.         period  following  END.  The  period is the  signal  to  the
  97.         compiler  that  it  has reached the end  of  the  executable
  98.         statements and is therefore finished compiling. Every Pascal
  99.         program  will have one,  and only one period in it and  that
  100.         one period will be at the end of the program. I must qualify
  101.         that  statement  in  this regard,  a period can be  used  in
  102.         comments,  and in text to be output.  In fact there are some
  103.         data  formats that require using a period as part  of  their
  104.         structure.  The statement is true however that there is only
  105.         one period in the executable part of a Pascal program. Think
  106.         of  a Pascal program as one long sentence with one period at
  107.         the end.
  108.  
  109.             That should pretty well describe our first program.  Now
  110.         it is time to compile and run it. To do so you must exit the
  111.         editor,   using  Ctrl-K-D,  unless  you  modified  the  exit
  112.         command.  Then compile the program,  and finally run it  and
  113.         observe the result.
  114.  
  115.             Since  that  program  didn't do much,  it was  not  very
  116.         interesting, so let's get one that does something.
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.                                  Page 7
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.                    CHAPTER 2 - Getting started in Pascal
  134.  
  135.  
  136.                        A PROGRAM THAT DOES SOMETHING
  137.  
  138.             Load the Pascal program WRITESM and edit it.   The  name
  139.         is  sort  of  cryptic for "Write Some" and it  will  give  a
  140.         little   output  to  the  monitor.   The  program  name   is
  141.         "kitty_cat"  which says nothing about the program itself but
  142.         is  any identifier we choose to make it.  We still have  the
  143.         BEGIN  and END to define the main program area  followed  by
  144.         the period.  However,  now we have two additional statements
  145.         between the BEGIN and END.  WRITELN is another reserved word
  146.         and  it is probably not surprising that it means to write  a
  147.         line of data somewhere. Without a modifier, (to be explained
  148.         in due time),  it will write to the default device which, in
  149.         the  case of our IBM compatible,  is the video display.  The
  150.         data within the parentheses is the data to be output to  the
  151.         display and although there are many possibilities of display
  152.         information,  we will restrict ourselves to the simplest for
  153.         the time being.  Any data between apostrophes will simply be
  154.         output as text information.
  155.  
  156.             Notice  the semicolon at the end of each line.  This  is
  157.         the statement separator referred to earlier and tells Pascal
  158.         that  this  line is complete as it stands,  nothing more  is
  159.         coming that could be considered part of this statement. This
  160.         program will output the two lines of text and stop.  Now  it
  161.         is time to go try it.  Exit the editor, then compile and run
  162.         the program.
  163.  
  164.             You  should  now get the two lines output to  the  video
  165.         display  every  time  you run it.  When you  grow  bored  of
  166.         running WRITESM lets go on to another example.
  167.  
  168.                      ANOTHER PROGRAM WITH MORE OUTPUT
  169.  
  170.             Load and edit WRITEMR.  This new program has three lines
  171.         of  output  but the first two are different because  another
  172.         reserved  word  is introduced to  us,  namely  WRITE.  WRITE
  173.         causes  the text to be output in exactly the same manner  as
  174.         WRITELN, but WRITE does not cause a carriage return. WRITELN
  175.         causes its output to take place then returns the  "carriage"
  176.         to  the first character of the next line.  The end result is
  177.         that all three of the lines will be output on the same  line
  178.         when the program is run. Notice that there is a blank at the
  179.         end  of  each of the first two lines so that the  formatting
  180.         will look nice. Exit the editor now and try the new program.
  181.  
  182.             It is time to confess to a little lie. WRITELN and WRITE
  183.         are   not  actually  reserved  words,   they  are   actually
  184.         predefined  functions which we have not discussed  yet.  For
  185.         the time being,  it is easiest to think of them as  reserved
  186.  
  187.  
  188.  
  189.                                  Page 8
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.                    CHAPTER 2 - Getting started in Pascal
  200.  
  201.  
  202.         words.  When  we get to the proper point,  we will  redefine
  203.         them properly.
  204.  
  205.             Now  might be a good time for you to go back to  editing
  206.         WRITEMR and add a few more output commands to see if they do
  207.         what  you think they should do.  When you tire of  that,  we
  208.         will  go on to the next file and learn about comments within
  209.         your Pascal program.
  210.  
  211.                       ADDING COMMENTS IN THE PROGRAM
  212.  
  213.             The  file named PASCOMS is similar to the others  except
  214.         that  comments  have  been added to  illustrate  their  use.
  215.         Pascal  defines  comments as anything between (* and  *)  or
  216.         anything  between  {  and  }.  Originally  only  the  wiggly
  217.         brackets  were defined but since many keyboards didn't  have
  218.         them available, the parenthesis star combination was defined
  219.         as an extension and is probably universal by now, so you can
  220.         use either. Most of the comments are self explanatory except
  221.         for the one within the code. Since comments can go from line
  222.         to line, the two lines that would print "send money" are not
  223.         Pascal code but are commented out. Try compiling and running
  224.         this  program,  then  edit the comments out  so  that  "send
  225.         money" is printed also.
  226.  
  227.             When  you have successfully modified and run the program
  228.         with  comments,  we  will go on to explain  good  formatting
  229.         practice  and  how  Pascal actually  searches  through  your
  230.         source file (Pascal program) for its executable statements.
  231.  
  232.                          GOOD FORMATTING PRACTICE
  233.  
  234.             Edit  GOODFORM now to see an example of good  formatting
  235.         style.  It  is important to note that Pascal doesn't give  a
  236.         hoot  where you put carriage returns or how many blanks  you
  237.         put  in when a blank is called for as  a  delimiter.  Pascal
  238.         only  uses  the  combination of reserved words  and  end-of-
  239.         statement  semicolons to determine the logical structure  of
  240.         the  program.   Since  we  have  really  only  covered   two
  241.         executable  statements,  I  have used them to build  a  nice
  242.         looking  program that can be easily understood at a  glance.
  243.         Compile and run this program to see that it really does what
  244.         you think it should do. 
  245.  
  246.                        VERY POOR FORMATTING PRACTICE
  247.  
  248.             Edit   UGLYFORM  now  to  see  an  example  of  terrible
  249.         formatting style.  It is not really apparent at a glance but
  250.         the  program you are looking at is exactly the same  program
  251.         as the last one. Pascal doesn't care which one you ask it to
  252.         run because to Pascal,  they are identical.  To you they are
  253.  
  254.  
  255.                                  Page 9
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.                    CHAPTER 2 - Getting started in Pascal
  266.  
  267.  
  268.         considerably different,  and the second one would be a  mess
  269.         to try to modify or maintain sometime in the future.
  270.  
  271.             UGLYFORM  should be a good indication to you that Pascal
  272.         doesn't  care about programming style or form.  Pascal  only
  273.         cares  about  the structure,  including reserved  words  and
  274.         delimiters such as blanks and semicolons.  Carriage  returns
  275.         are  completely  ignored as are extra blanks.  You  can  put
  276.         extra blanks nearly anywhere except within reserved words or
  277.         variable  names.  You  should pay attention  to  programming
  278.         style  but don't get too worried about it yet.  As time goes
  279.         by you will develop a style of statement indentation, adding
  280.         blank  lines  for clarity,  and clear commenting  of  Pascal
  281.         source  code.  Programs  are available to read  your  source
  282.         code,  and  put  it in a "pretty" format,  but that  is  not
  283.         important now.
  284.  
  285.             Not only is the form of the program important, the names
  286.         used  for variables can be very helpful or hindering  as  we
  287.         can see in the next chapter. Feel free to move things around
  288.         and modify the format of any of the programs we have covered
  289.         so far and when you are ready, we will start on variables in
  290.         the next chapter.
  291.  
  292.              Be sure you compile and run UGLYFORM.
  293.  
  294.                            PROGRAMMING EXERCISES
  295.  
  296.         1.  Write  a  program that displays your name on  the  video
  297.             monitor. 
  298.  
  299.         2.  Modify  your program to display your name and address on
  300.             one  line,  then  modify it by changing the  WRITE's  to
  301.             WRITELN's so that the name and address are on  different
  302.             lines.
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.                                  Page 10
  322.